home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / ButtonGroup.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  3.6 KB  |  125 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ButtonGroup.java    1.22 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.event.*;
  17. import java.util.Vector;
  18. import java.util.Enumeration;
  19. import java.io.Serializable;
  20.  
  21. /**
  22.  * This class is used to create a multiple-exclusion scope for
  23.  * a set of buttons. Creating a set of buttons with the
  24.  * same ButtonGroup object means that turning "on" one of those buttons 
  25.  * turns off all other buttons in the group. A ButtonGroup can be used with
  26.  * sets of JButton, JRadioButton, or JRadioButtonMenuItem objects.
  27.  * <p>
  28.  * Initially, all buttons in the group are unselected. Once any button is
  29.  * selected, one button is always selected in the group. There is no way
  30.  * to turn a button programmatically to "off", in order to clear the button
  31.  * group. To give the appearance of "none selected", add an invisible radio
  32.  * button to the group and then programmatically select that button to 
  33.  * turn off all the displayed radio buttons. For example, a normal button
  34.  * with the label "none" could be wired to select the invisible radio button.
  35.  * <p>
  36.  * <strong>Warning:</strong>
  37.  * Serialized objects of this class will not be compatible with 
  38.  * future Swing releases.  The current serialization support is appropriate
  39.  * for short term storage or RMI between applications running the same
  40.  * version of Swing.  A future release of Swing will provide support for
  41.  * long term persistence.
  42.  *
  43.  * @version 1.22 08/28/98
  44.  * @author Jeff Dinkins
  45.  */
  46. public class ButtonGroup implements Serializable {
  47.  
  48.     // the list of buttons participating in this group
  49.     protected Vector buttons = new Vector();
  50.  
  51.     /**
  52.      * The current choice.
  53.      */
  54.     ButtonModel selection = null;
  55.  
  56.     /**
  57.      * Creates a new ButtonGroup.
  58.      */
  59.     public ButtonGroup() {}
  60.  
  61.     /**
  62.      * Adds the button to the group.
  63.      */ 
  64.     public void add(AbstractButton b) {
  65.         if(b == null) {
  66.             return;
  67.         }
  68.         buttons.addElement(b);
  69.         if(selection == null && b.isSelected()) {
  70.             selection = b.getModel();
  71.         }
  72.         b.getModel().setGroup(this);
  73.     }
  74.  
  75.     /**
  76.      * Removes the button from the group.
  77.      */ 
  78.     public void remove(AbstractButton b) {
  79.         if(b == null) {
  80.             return;
  81.         }
  82.         buttons.removeElement(b);
  83.         if(b.getModel() == selection) {
  84.             selection = null;
  85.         }
  86.         b.getModel().setGroup(null);
  87.     }
  88.  
  89.     /**
  90.      * Return all the buttons that are participating in
  91.      * this group.
  92.      */
  93.     public Enumeration getElements() {
  94.         return buttons.elements();
  95.     }
  96.  
  97.     /**
  98.      * Return the selected button model.
  99.      */
  100.     public ButtonModel getSelection() {
  101.         return selection;
  102.     }
  103.  
  104.     /**
  105.      * Sets the selected value for the button.
  106.      */
  107.     public void setSelected(ButtonModel m, boolean b) {
  108.         if(b && m != selection) {
  109.             ButtonModel oldSelection = selection;
  110.             selection = m;
  111.             if(oldSelection != null) {
  112.                 oldSelection.setSelected(false);
  113.             }
  114.         } 
  115.     }
  116.  
  117.     /**
  118.      * Returns the selected value for the button.
  119.      */
  120.     public boolean isSelected(ButtonModel m) {
  121.         return (m == selection);
  122.     }
  123.  
  124. }
  125.